
import org.apache.commons.math3.distribution.ChiSquaredDistribution;
import java.util.Scanner;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author umannamu
 */
public class UjiJarqueBerra extends UjiDistribusi {

    private ChiSquaredDistribution chi = new ChiSquaredDistribution(2);
    private double sigma4 = 0;
    private double sigma3 = 0;
    private double sigma2 = 0;
    private double selisih, pangkat3, pangkat2, pangkat4, mean, size;
    private double sum = 0;
    Scanner input = new Scanner(System.in);

    @Override
    public void setStatTestResult() {
        //skewness
        mean = MeanStatistik.hitung(data);
        size = data.getSize();
        for (int i = 0; i < size; i++) {
            selisih = data.getData(i) - mean;
            pangkat4 = (double) Math.pow(selisih, 4);
            pangkat3 = (double) Math.pow(selisih, 3);
            pangkat2 = (double) Math.pow(selisih, 2);
            sigma2 += pangkat2;
            sigma3 += pangkat3;
            sigma4 += pangkat4;
        }
        //skewness
        double skewness = (sigma3 / size) / (Math.pow(sigma2, 1.5) / Math.pow(size, 1.5));
        //kurtosis
        double kurtosis = (sigma4 / size) / (Math.pow(sigma2, 2) / Math.pow(size, 2));
        //JB
        double JB = (size * ((Math.pow(skewness, 2) / 6) + (Math.pow((kurtosis - 3), 2) / 24)));
        setStatTestResult(JB);
    }

    public void ambilKeputusan() {
        System.out.print("Masukkan tingkat signifikansi (sig-level)\t: ");
        setSigLevel(input.nextDouble());
        setpValue(chi.cumulativeProbability(getStatTestResult()));
        System.out.print("Dengan p-value\t: " + getpValue() + " dan tingkat signifikansi 5% atau 0.05, maka ");
        if (getpValue() <= getSigLevel()) {
            System.out.print("tolak H0.\n" + "Kesimpulan\t: Tidak cukup bukti untuk mengatakan data berdistribusi normal.");
        } else {
            System.out.print("gagal tolak H0.\n" + "Kesimpulan\t: Cukup bukti untuk mengatakan data berdistribusi normal.");
        }
    }

    @Override
    public void output() {
        data.print();
        System.out.println("H0 :\tData berdistribusi normal." + "\nH1 :\tData tidak berdistribusi normal.");
        System.out.println("Analisa pengujian distribusi data Anda menggunakan uji Jarque-Bera" + " dengan statistik uji sebesar :" + getStatTestResult());
        ambilKeputusan();
    }

    @Override
    public void userInput(InputOutput io) {
        this.io = io;
        setStatTestResult();
    }

    @Override
    public void userInput() {
        setStatTestResult();
    }
}
